<template>
    <div class="wxLogin">
        <van-nav-bar
                title="微信登录"
                left-arrow
                @click-left="back">
        </van-nav-bar>
        <div class="authorizeResult text-center-pk">
            <img :src="iconUrl" alt="icon">
            <div>{{resultTips}}</div>
            <div v-if="!code" class="btnWrap">
                <van-button round type="default" class="btn back" @click="back">返回</van-button>
                <van-button round type="danger" class="btn reLogin" @click="reLogin">重新登录</van-button>
            </div>
        </div>
    </div>
</template>

<script>
    //根据url 是否携带code字段判断微信登录是否授权成功
    //import iconSuccess from "@img/icon-success.png";
    //import iconFail from ("@img/icon_succes.png");
    export default {
        name: 'wxLogin',
        components: {},

        data() {
            return {
                code: this.$route.query.code,
                prePath: this.$route.query.prePath ? this.$route.query.prePath : "/index",
                iconUrl: require('@img/icon-success.png'),
                resultTips: "授权成功，正在登录",
                queryObjFmt: {}
            }
        },

        created() {
            if(this.code) {
                this.iconUrl = require("@img/icon-success.png");
                this.resultTips = "授权成功，正在登录";
                this.authLogin();
            }else {
                this.iconUrl = require('@img/icon-fail.png');
                this.resultTips = "微信授权失败";
            }
            for(let key in this.$route.query) {
                if(key !== "code" && key !== "prePath") {
                    this.queryObjFmt[key] = this.$route.query[key]
                }
            }
        },
        watch: {
        },
        methods: {
            //微信授权成功，登录app
            authLogin() {
                this.$toast.loading({
                    message: "正在登录..."
                });
                this.$api.loginWx({
                    "code": this.code,
                    "device": "WEB",
                    "deviceNo": "WEB",
                    //"inviteNumber": this.inviteNum
                }).then( res => {
                    this.$toast.clear();
                    let result = this.$resData(res);
                    if(result) {
                        //this.$router.push({name: "index", params: result})
                        this.$router.push({path: this.prePath, query: this.queryObjFmt})
                    }
                })
            },
            back() {
                this.$router.push({path: this.prePath, query: this.queryObjFmt})
            },
            reLogin() {
                //待优化，重新登录的时候将参数带上,目前仅有微信一次授权成功，会将参数带上
                this.$router.push({path: "/login"})
            }
        }
    }
</script>

<style lang="scss" scoped>
    .wxLogin {
        .authorizeResult {
            margin-top: 50px;
            font-size: 20px;
            img {
                width: 4em;
                height: 4em;
                margin-bottom: 10px;
            }
            .btnWrap {
                margin-top: 20px;
                display: flex;
                justify-content: center;
                .btn {
                    width: 8em;
                    margin: 10px 20px;
                }
            }
        }
    }
</style>
